package com.wst.business.controller;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.wst.system.interceptor.encrypt.EncryptDecrypt;
import com.wst.system.interceptor.encrypt.EncryptDecryptTime;
import com.wst.system.security.IsSecurity;
import com.wst.system.server.entity.SysFile;
import com.wst.system.server.service.SysFileService;
import com.wst.system.util.ResultUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wst.system.config.exception.GlobalExcetion;
import java.io.Serializable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.wst.business.service.SubTitleService;
import com.wst.business.entity.SubTitle;
import com.wst.business.entity.vo.SubTitleVo;
import com.wst.system.server.entity.vo.PageSearchVo;
import org.springframework.validation.annotation.Validated;

import java.util.ArrayList;
import java.util.List;
import com.wst.system.config.PageData;
import com.wst.system.server.entity.vo.DelManyDataVo;
import org.springframework.web.multipart.MultipartFile;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author X.I.O
 * @since 2025-03-21
 */

@RestController
@Api(tags = {"短文的服务类"})
@RequestMapping("/sub-title")
public class SubTitleController  {

        @Autowired
        private SubTitleService subTitleService;

        @Autowired
        private SysFileService sysFileService;


        @IsSecurity(isSecurity = false)
        @PostMapping("/encrypt/saveorupdate")
        @EncryptDecrypt
        @ApiOperation(value = "短文的新增/更新,加密接口",notes = "的新增/更新加密接口")
        public JSONObject saveOrUpdate(SubTitleVo subTitleVo) throws GlobalExcetion {
            subTitleService.saveOrUpdateSubTitle(subTitleVo);
            return ResultUtil.successInfo("操作成功");
        }



        @IsSecurity(isSecurity = false)
        @PostMapping("/uploadImg")
        public JSONObject uploadImg(@RequestParam MultipartFile[] files) throws Exception {
            if(files == null || files.length == 0){
                return ResultUtil.successInfo("上传文件不得为空");
            }
            JSONArray array=new JSONArray();
            for (int i = 0; i < files.length ; i++) {
                MultipartFile file = files[i];
                SysFile sysFile=sysFileService.uploadFile(file);
                JSONObject jsonObject=new JSONObject();
                jsonObject.put("id",sysFile.getId());
                jsonObject.put("fileName",sysFile.getFileName());
                jsonObject.put("url",sysFile.getTempUrl());
                array.add(jsonObject);
            }
            return ResultUtil.successInfo(array);
        }


        @PostMapping("/saveorupdate")
        @ApiOperation(value = "短文的新增/更新接口",notes = "的新增接口")
        public JSONObject save(@RequestBody @Validated SubTitleVo subTitleVo) throws GlobalExcetion{
                subTitleService.saveOrUpdateSubTitle(subTitleVo);
            return ResultUtil.successInfo("操作成功");
        }





        @DeleteMapping("/delbyid")
        @ApiOperation(value = "短文的删除接口",notes = "的删除接口")
        public JSONObject delete(@RequestParam("id") Serializable id) throws GlobalExcetion{
            boolean flag = subTitleService.removeById(id);
            if(flag){
                return ResultUtil.successInfo("删除成功");
            }else {
                return ResultUtil.errorInfo("删除失败");
            }
        }

        @PostMapping("/del/batch")
        @ApiOperation(value = "短文的批量删除接口",notes = "的批量删除接口")
        public JSONObject deleteBatch(@RequestBody DelManyDataVo vo) throws GlobalExcetion {
            boolean flag =  subTitleService.removeByIds(vo.getIds());
            if(flag){
                return ResultUtil.successInfo("删除成功");
            }else {
                return ResultUtil.errorInfo("删除失败");
            }
        }

        @EncryptDecryptTime
        @IsSecurity(isSecurity = false)
        @PostMapping("/getall")
        @ApiOperation(value = "短文的获取全部接口",notes = "的获取全部接口")
        public JSONObject findAll() throws GlobalExcetion {
            List<SubTitle> list= subTitleService.list();
            return ResultUtil.successInfo(list);
        }

        @GetMapping("/getbyid")
        @ApiOperation(value = "短文的获取一个接口",notes = "的获取一个接口")
        public JSONObject findOne(@RequestParam("id") Serializable id) throws GlobalExcetion {
            SubTitle data= subTitleService.getById(id);
            return ResultUtil.successInfo(data);
        }

        @PostMapping("/page")
        @ApiOperation(value = "短文的分页接口",notes = "的分页接口")
        public JSONObject findPage(@RequestBody @Validated PageSearchVo pageSearchVo) throws GlobalExcetion {
            IPage<SubTitle> data=subTitleService.page(pageSearchVo);
            return ResultUtil.successInfoTable(data.getTotal(),data.getRecords());
        }

        @PostMapping("/encrypt/page")
        @EncryptDecrypt
        @ApiOperation(value = "短文的分页加密接口",notes = "的分页加密接口")
        public JSONObject findPageEncrypt(PageSearchVo pageSearchVo) throws GlobalExcetion {
            IPage<SubTitle> data=subTitleService.page(pageSearchVo);
            return ResultUtil.successInfoTable(data.getTotal(),data.getRecords());
        }
}

